package com.nz2004.deaddelayedqueue.mq;

import com.nz2004.deaddelayedqueue.config.RabbitMQConfig;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class DelayedListener {

    @RabbitListener(queues = {RabbitMQConfig.DELAYED_QUEUE})
    public void listen(Message message, Channel channel) throws IOException {
        // 输出消息
        System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + ",消息是:" + new String(message.getBody()));
        // 手动ack
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }
}
